/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.chooser.view;

import java.util.Collection;
import offset.nodes.client.chooser.model.FindNodes;
import offset.nodes.client.chooser.model.FolderNode;
import offset.nodes.client.model.Server;
import offset.nodes.client.view.SimpleDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class QueryResultChooserDialog extends SimpleDialog {
    SimpleDialog dialog = null;
    QueryResultChooserPanel panel = null;
    
    public QueryResultChooserDialog(java.awt.Frame parent, boolean modal, Server server, String query) throws Exception {
        super(parent, modal);
        panel = new QueryResultChooserPanel(executeQuery(server, query));
        addContent(panel);
        panel.setContainer(this);
        enableOkButton(false);
        setSize(500,350);
    }

    /**
     * Return the path of the selected result node in case of OK
     *
     * @return
     */
    public String getPath() {
        if (panel.getSelectedNode() != null)
            return panel.getSelectedNode().getPath();

        return null;
    }

    /**
     * Return the uuid of the selected result ode in case of OK
     * @return
     */
    public String getUuid() {
        if (panel.getSelectedNode() != null)
            return panel.getSelectedNode().getUuid();

        return null;
    }

    protected Collection<FolderNode> executeQuery(Server server, String query) throws Exception {
        FindNodes.Request request = new FindNodes.Request();
        request.setFilter(query);
        request.setRoot("");
        FindNodes.Response response = (FindNodes.Response) server.sendRequest(request);

        return response.getResultNodes();
    }
}
